package com.stone.sys.service;

/**
 * <pre>
 * 
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import com.stone.sys.pojo.TRole;
import com.stone.sys.pojo.TRoleMenu;
import com.stone.sys.pojo.TUserRole;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class RoleService extends BaseService {

	/**
	 */
	@SuppressWarnings("unchecked")
	public void save(TRole role,String menuID) throws AppException {
		
		String[] sid = menuID.split(",");
		
		// 新建时
		if ( role.getId() == null ||  role.getId() == 0) {
			
			// 角色表
			//role.setId(SeqUtil.getNextKey("SEQ_SYS"));
			super.save(role);
			
			// 角色菜单
			for (String s : sid) {
				TRoleMenu rm = new TRoleMenu();
				//rm.setId(SeqUtil.getNextKey("SEQ_SYS"));
				rm.setRoleId(role.getId());
				rm.setMenuId(Long.parseLong(s));
				super.save(rm);
			}
			
			// 修改时
		} else {
			
			// 修改角色表
			super.update(role);
			
			// 查询角色菜单原有的数据
			String whereRole = " rm.roleId = '" + role.getId() + "'";
			
			// 角色对应的菜单
			List<HashMap<String,Object>> list = this.getListByDHql("rm.id as id,rm.menuId as menuId", 
											whereRole,// + and rm.roleId = '" + id + "'",
											null,
											"",
											"TRoleMenu rm ");
		
			boolean flag = false;
			
			// 新角色菜单
			for (String s : sid) {
				
				// 旧角色菜单
				for (HashMap<String,Object> m : list) {
					if (s.equals(String.valueOf(m.get("menuId")))) {
						flag = true;	
						break;
					}
				}
				if (!flag) {
					TRoleMenu rm = new TRoleMenu();
					//rm.setId(SeqUtil.getNextKey("SEQ_SYS"));
					rm.setRoleId(role.getId());
					rm.setMenuId(Long.parseLong(s));
					super.save(rm);
				}
				
				flag = false;
			}
			
			// 删除原来有后来去掉的菜单
			flag = false;
			for (HashMap<String,Object> m : list) {

				for (String s : sid) {
					if (s.equals(String.valueOf(m.get("menuId")))) {
						flag = true;	
						break;
					}
				}
				// 删除多余菜单
				if (!flag) {
					super.deleteAll(String.valueOf(m.get("id")), "TRoleMenu");
				}
				flag = false;
			}
		}
	}
	
	/**
	 * 删除角色同时删除角色菜单数据
	 */
	public void delete(Long id) throws AppException {
		// 删除角色菜单
		super.deleteByDHql(" roleId = ? ", new Object[]{id}, "TRoleMenu");
		// 删除角色用户
		super.deleteByDHql(" roleId = ? ", new Object[]{id}, "TUserRole");
		// 删除角色
		super.delete(id, "TRole");

	}
	
	/**
	 * 用户授权及取消
	 */
	@SuppressWarnings("unchecked")
	public void saveUserRole(String roleID,String userID, String flag) throws AppException {
		
		// 要操作的人员ID数组
		String[] sid = userID.split(",");
		
		// 授权时
		if ("1".equals(flag)) {
			
			// 角色对应的已授权人员
			List<HashMap<String,Object>> list = this.getListByDHql("ur.id as id,ur.userId as userId", 
											"ur.roleId = '" + roleID + "'",
											null,
											"",
											"TUserRole ur ");
		
			boolean f = false;
			
			// 需要授权的人员
			for (String s : sid) {
				
				f = false;
				
				// 角色中已有人员
				for (HashMap<String,Object> m : list) {
					if (s.equals(String.valueOf(m.get("userId")))) {
						f = true;	
						break;
					}
				}
				// 如果没有就加入
				if (!f) {
					TUserRole rm = new TUserRole();
					//rm.setId(SeqUtil.getNextKey("SEQ_SYS"));
					rm.setRoleId(Long.parseLong(roleID));
					rm.setUserId(Long.parseLong(s));
					super.save(rm);
				}
			}
			
			// 取消授权
		} else {
			
			if(userID.endsWith(",")) {
				userID = userID.substring(0,userID.length()-1);
			}
			
			super.deleteByDHql(" userId in (" + userID + ") and roleId = " + roleID, null, "TUserRole");
		}
	}

	/**
	 * 执行Hql查询查询
	 * 
	 * @param hql  查询语句
	 * @return List 结果集
	 */
	public List findListByHql(String hql)  {
			return super.findListByHql(hql);
	}
	
	
	/**
	 * 根据ID查询记录
	 * 
	 * @param id ID
	 * @param objectFullName POJO名 包括包路径
	 * @return Object
	 */
	public Object findById(Long id, String objectFullName)  {
		return super.findById(id, objectFullName);
	}
	
	
	/**
	 * 保存pojo对象
	 * 
	 * @param transientInstance pojo对象
	 */
	public void save(Object transientInstance)  {
			super.save(transientInstance);
	}
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void update(Object transientInstance)  {
			super.update(transientInstance);
	}
	
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void doDelAll(final String ids,final String objectSimpleName)  {
			super.doDelAll(ids,objectSimpleName);
	}
	
	/**
	 * 通过SQL查询列表，返回每条是一个HashMap<String,Object>
	 * @param sql 查询SQL语句
	 * @return list 返回列表
	 */
	public List<HashMap<String,Object>> findListBySql(String sql){
	
		List<HashMap<String,Object>> list =  (List<HashMap<String,Object>>) super.findListBySql(sql);
		return list;
	}

	
	/**
	 * 此方法是用来动态获取列表，可以多表关联
	 * 
	 * @param cols 查询列名
	 * @param conditions 查询条件
	 * @param vals 查询条件对应值
	 * @return clause order by, group by,having等子句
	 * @param objectSimpleName 
	 */
	public List getListByDHql(final String cols, final String conditions,
			final Object[] vals, final String clause,
			final String objectSimpleName)  {
		
		return super.getListByDHql(cols, conditions, vals, clause, objectSimpleName);
	}
	
	/**
	 * 根据ID删除记录
	 * 
	 * @param id 删除记录的ID
	 * @param objectSimpleName
	 */
	public void delete(Long id, String objectSimpleName){
			super.delete(id, objectSimpleName);
	}
	/**
	 * 通过HQL修改记录
	 * 
	 * @param hql 修改的HQL语句
	 * @param values HQL语句对应值
	 */
	public void updateByHQL(String hql, Object[] values){
		super.updateByHQL(hql,values);
	}

}
